﻿using Automatic.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Automatic.Controls
{
    /// <summary>
    /// RaderChart.xaml 的交互逻辑
    /// </summary>
    public partial class RaderChart : UserControl
    {
        public RaderChart()
        {
            InitializeComponent();
            this.SizeChanged += RaderChart_SizeChanged;
        }

        private void RaderChart_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Refresh();
        }

        public ObservableCollection<RaderSeriesModel> ItemsSources
        {
            get { return (ObservableCollection<RaderSeriesModel>)GetValue(ItemsSourcesProperty); }
            set { SetValue(ItemsSourcesProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourcesProperty =
            DependencyProperty.Register("ItemsSources", typeof(ObservableCollection<RaderSeriesModel>), typeof(RaderChart), new PropertyMetadata(null, new PropertyChangedCallback(OnPropertyChanged)));

        public static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                if (d != null)
                {
                    (d as RaderChart).Refresh();
                }
                // (e.NewValue as ObservableCollection<RaderSeriesModel>).CollectionChanged += (o, e) =>
                // {
                //     (d as RaderChart).Refresh();
                // };
            }

        }


        private void Refresh()
        {
            if (ItemsSources != null)
            {
                this.mainCanvas.Children.Clear();
                p1.Points.Clear();
                p2.Points.Clear();
                p3.Points.Clear();
                p4.Points.Clear();
                p5.Points.Clear();


                double size = Math.Min(RenderSize.Width, RenderSize.Height);
                LayoutRoot.Width = size;
                LayoutRoot.Height = size;

                double raduis = size / 2;
                double step = 360.0 / ItemsSources.Count;
                for (int i = 0; i < ItemsSources.Count; i++)
                {
                    p1.Points.Add(new Point(
                        raduis + (raduis - 20) * Math.Cos((step * i - 90) * Math.PI / 180),
                        raduis + (raduis - 20) * Math.Sin((step * i - 90) * Math.PI / 180)));

                    p2.Points.Add(new Point(
                        raduis + (raduis - 20) * Math.Cos((step * i - 90) * Math.PI / 180) * 0.25,
                        raduis + (raduis - 20) * Math.Sin((step * i - 90) * Math.PI / 180) * 0.25));

                    p3.Points.Add(new Point(
                        raduis + (raduis - 20) * Math.Cos((step * i - 90) * Math.PI / 180) * 0.5,
                        raduis + (raduis - 20) * Math.Sin((step * i - 90) * Math.PI / 180) * 0.5));

                    p4.Points.Add(new Point(
                        raduis + (raduis - 20) * Math.Cos((step * i - 90) * Math.PI / 180) * 0.75,
                        raduis + (raduis - 20) * Math.Sin((step * i - 90) * Math.PI / 180) * 0.75));

                    p5.Points.Add(new Point(
                        raduis + (raduis - 20) * Math.Cos((step * i - 90) * Math.PI / 180) * ItemsSources[i].Value / 100,
                        raduis + (raduis - 20) * Math.Sin((step * i - 90) * Math.PI / 180) * ItemsSources[i].Value / 100));

                    Line line = new Line()
                    {
                        Stroke = new SolidColorBrush(Color.FromArgb(34, 255, 255, 255)),
                        StrokeThickness = 1,
                        X1 = raduis,
                        Y1 = raduis,
                        X2 = raduis + (raduis - 20) * Math.Cos((step * i - 90) * Math.PI / 180),
                        Y2 = raduis + (raduis - 20) * Math.Sin((step * i - 90) * Math.PI / 180),
                    };

                    mainCanvas.Children.Add(line);

                    TextBlock textBlock = new TextBlock()
                    {
                        Width = 60,
                        FontSize = 10,
                        TextAlignment = TextAlignment.Center,
                        Text = ItemsSources[i].Header,
                        Foreground = new SolidColorBrush(Color.FromArgb(100, 255, 255, 255)),
                    };
                    textBlock.SetValue(Canvas.LeftProperty, raduis + (raduis - 10) * Math.Cos((step * i - 90) * Math.PI / 180) - 30);
                    textBlock.SetValue(Canvas.TopProperty, raduis + (raduis - 10) * Math.Sin((step * i - 90) * Math.PI / 180) - 8);
                    mainCanvas.Children.Add(textBlock);
                }

            }

        }


    }
}